﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Quarto
{
    class Menu : Microsoft.Xna.Framework.Game
    {
        Sprite titre;
        Sprite annuler;
        Sprite valider;
        Sprite[] piece;
        Sprite Deux_Joueurs;
        Sprite IA_Facile;
        Sprite IA_Difficile;
        Sprite selection_motif;
        Sprite selection_IA;
        int[] motif_selectionne;
        bool actif;
        int ia;
        bool envoyer_motifs;

        public const int TAILLE_titre = 250;
        public const int TAILLE_BOUTON = 200;
        public const int TAILLE_CHOIX = 150;
        public const int TAILLE_piece_MENU = 100;

        public Sprite Titre
        {
            get { return titre; }
            set { titre = value; }
        }
        public Sprite Annuler
        {
            get { return annuler; }
            set { annuler = value; }
        }
        public Sprite Valider
        {
            get { return valider; }
            set { valider = value; }
        }
        public Sprite[] Piece
        {
            get { return piece; }
            set { piece = value; }
        }
        public int[] Motif_selectionnee
        {
            get { return motif_selectionne; }
            set { motif_selectionne = value; }
        }
        public Sprite Selection_IA
        {
            get { return selection_IA; }
            set { selection_IA = value; }
        }
        public int Ia
        {
            get { return ia; }
            set { ia = value; }
        }
        public bool Actif
        {
            get { return actif; }
            set { actif = value; }
        }
        public bool Envoyer_motifs
        {
            get { return envoyer_motifs; }
            set { envoyer_motifs = value; }
        }
        public Menu(ContentManager Content)
        {
            this.piece = new Sprite[6];
            this.IsMouseVisible = true;
            this.motif_selectionne = new int[3];
            for(int i = 0; i<3; i++)
            {
                this.motif_selectionne[i] = -1;
            }
            this.actif = true;
            this.ia = 0;
            this.envoyer_motifs = false;
            this.titre = new Sprite(Content.Load<Texture2D>("Titre"), 275, 10);
            this.annuler = new Sprite(Content.Load<Texture2D>("Annuler"), 136, 525);
            this.valider = new Sprite(Content.Load<Texture2D>("Valider"), 472, 525);
            this.Deux_Joueurs = new Sprite(Content.Load<Texture2D>("2_Joueurs"), 93, 425);
            this.IA_Facile = new Sprite(Content.Load<Texture2D>("IA_Facile"), 315, 425);
            this.IA_Difficile = new Sprite(Content.Load<Texture2D>("IA_Difficile"), 547, 425);
            this.selection_motif = new Sprite(Content.Load<Texture2D>("Selection_motif"), 0, 0);
            this.selection_IA = new Sprite(Content.Load<Texture2D>("Selection_IA"), 0, 0);
            this.piece[0] = new Sprite(Content.Load<Texture2D>("Piece_1"), 130, 150);
            this.piece[1] = new Sprite(Content.Load<Texture2D>("Piece_2"), 350, 150);
            this.piece[2] = new Sprite(Content.Load<Texture2D>("Piece_3"), 570, 150);
            this.piece[3] = new Sprite(Content.Load<Texture2D>("Piece_4"), 130, 300);
            this.piece[4] = new Sprite(Content.Load<Texture2D>("Piece_5"), 350, 300);
            this.piece[5] = new Sprite(Content.Load<Texture2D>("Piece_6"), 570, 300);
            for (int i = 0; i < 6; i++)
            {
                this.piece[i].Dimentions.Width = TAILLE_piece_MENU;
                this.piece[i].Dimentions.Height = TAILLE_piece_MENU;
            }
        }
        public void Update(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Vector2 position_clic = Controle.attente_clic_souris();
            if (position_clic.X != -1 && position_clic.Y != -1)
            {
                if (this.Annuler.test_clic(position_clic))
                {
                    for (int i = 0; i < 3; i++)
                    {
                        this.Motif_selectionnee[i] = -1;
                    }
                }
                if (this.Valider.test_clic(position_clic))
                {
                    this.Actif = false;
                    this.envoyer_motifs = true;
                }
                if(this.Deux_Joueurs.test_clic(position_clic))
                {
                    this.ia = 0;
                }
                if (this.IA_Facile.test_clic(position_clic))
                {
                    this.ia = 1;
                }
                if (this.IA_Difficile.test_clic(position_clic))
                {
                    this.ia = 2;
                }
                for (int i = 0; i < 6; i++)
                {
                    if (this.Piece[i].test_clic(position_clic))
                    {
                        if (this.Motif_selectionnee[0] == -1)
                        {
                            this.Motif_selectionnee[0] = i;
                        }
                        else
                        {
                            if (this.Motif_selectionnee[1] == -1
                                && this.Motif_selectionnee[0] != i)
                            {
                                this.Motif_selectionnee[1] = i;
                            }
                            else
                            {
                                if (this.Motif_selectionnee[2] == -1
                                    && this.Motif_selectionnee[0] != i
                                    && this.Motif_selectionnee[1] != i)
                                {
                                    this.Motif_selectionnee[2] = i;
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            titre.Draw(spriteBatch, TAILLE_titre);
            annuler.Draw(spriteBatch, TAILLE_BOUTON);
            valider.Draw(spriteBatch, TAILLE_BOUTON);
            Deux_Joueurs.Draw(spriteBatch, TAILLE_CHOIX);
            IA_Facile.Draw(spriteBatch, TAILLE_CHOIX);
            IA_Difficile.Draw(spriteBatch, TAILLE_CHOIX);
            for (int i = 0; i < 6; i++ )
            {
                piece[i].Draw(spriteBatch, TAILLE_piece_MENU);
            }
            for (int i = 0; i < 3; i++ )
            {
                if (motif_selectionne[i] != -1)
                {
                    selection_motif.Dimentions.X = Piece[motif_selectionne[i]].Dimentions.X - 10;
                    selection_motif.Dimentions.Y = Piece[motif_selectionne[i]].Dimentions.Y - 10;
                    selection_motif.Draw(spriteBatch, TAILLE_piece_MENU + 10);
                }
            }
            switch(ia)
            {
                case 0:
                    selection_IA.Dimentions.X = Deux_Joueurs.Dimentions.X - 20;
                    selection_IA.Dimentions.Y = Deux_Joueurs.Dimentions.Y - 15;
                    break;
                case 1:
                    selection_IA.Dimentions.X = IA_Facile.Dimentions.X - 20;
                    selection_IA.Dimentions.Y = IA_Facile.Dimentions.Y - 15;
                    break;
                case 2:
                    selection_IA.Dimentions.X = IA_Difficile.Dimentions.X - 20;
                    selection_IA.Dimentions.Y = IA_Difficile.Dimentions.Y - 15;
                    break;
            }
            selection_IA.Draw(spriteBatch, TAILLE_CHOIX + 50);
            base.Draw(gameTime);
        }
    }
}
